/**
 * @file	HAL_UART_H.h
 * @author	chensj0621
 * @brief	
 * @version	0.1
 * @date	2021-11-11
 * @copyright Copyright (c) 2020, CHIPSEA Co., Ltd.
 * @note
 */
#ifndef HAL_UART_H
#define HAL_UART_H

#ifdef __cplusplus
extern "C"
{
#endif

#include "sdk_config.h"
#include "log.h"
#include "key.h"
#include "OSAL.h"
#include "gpio.h"
#include "flash.h"
#include "uart.h"
#include "pwrmgr.h"
#include "at_command.h"
#include "bleuart_service.h"
#include "att.h"
#include "multi_role.h"
/**********************宏定义**************************/
//#define FM_VERSION  "V03"
#define UART_BUF_SIZE	        1
#define UART_RX_BUFF_MAX        256

#define PIN_DETECTION_TIME	    30000
#define PIN_DEBONCE_TIME        10
#define WAKEUP_PIN	            P14
#define BT_STATUS_PIN	        P11

#define UART_REC_TIMEOUT	    50000

#define USR_UART_AT_DATA        0x0001
#define USR_UART_RCV_DATA       0x0002
#define USR_WAKEUP_PIN_DET      0x0004
#define USR_UART_SEND_DATA      0x0008
#define USR_EVT_OTHER           0x0010

#define	BAUD_FLASH_ADDR1		0x50000
#define	BAUD_FLASH_ADDR2		0x51000

#define AT_BRIDGE_NUM           5//允许桥接设备数量
/**********************结构体**************************/
typedef enum{
    UART_IDEL = 0x00,
    UART_RX   = 0x01,
    UART_RX_END = 0x02,
}uart_state_e;

typedef struct
{
    uint8_t in_role;//数据来源角色
    uint8_t in_role_index;//数据来源角色通道号
    uint8_t out_role;//数据去向角色
    uint8_t out_role_index;//数据去向角色通道号
    uint8_t bridge_enable;//桥接使能
}at_bridge_t;

#pragma pack (1)
typedef struct {
    uint16 flg;
    uint32 index;
    uint8  baud;
    uint8  advin;
    uint8  connin;
    uint8  power;
    uint8  autolink;
    uint8  scan_window;
    uint8  scan_interval;
    ble_service_t service;
    at_bridge_t bridge[AT_BRIDGE_NUM];
    uint8  autolinkaddr[MAX_SLAVE_NUM][B_ADDR_LEN];
    uint8  blemode;
    uint8  name[26];
    uint8  mac[6];
    uint8  check_sum;
} hal_uart_data_t;
#pragma pack ()

typedef struct 
{
    //uint8_t   wx_idx;
    uint8_t   rx_idx;
    uart_state_e   uart_state;
    uint8_t   rx_data[UART_RX_BUFF_MAX];

}hal_uart_t;
/**********************声明****************************/
extern hal_uart_data_t user_data;
extern const uint32_t baud_table[5];
extern const uint16_t advin_table[9];
extern const uint16_t connin_table[7];
extern const uint8_t power_table[5];
extern const uint8_t scan_window_table[5];
extern const uint32_t scan_interval_table[10];
/**********************函数****************************/

void HalPeripheral_Init(uint8 task_id);
uint16 HalPeripheral_ProcessEvent( uint8 task_id, uint16 events );
void ble_at_mode_init(void);
void user_data_init(void);
void user_data_save(void);
void user_at_init(void);
#ifdef __cplusplus
}
#endif

#endif /* HAL_UART_H */
